package org.xfy.ymcc.producer;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.xfy.common.constant.RocketConstants;
import org.xfy.ymcc.domain.PayOrder;

import java.util.Map;

@Component
public class OrderProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void notice(String message) {
        rocketMQTemplate.sendOneWay(RocketConstants.COURSE_SEND_SMS_TOPIC+":"
                +RocketConstants.COURSE_SEND_SMS_TAG,message);
    }

    public void sendTransaction(Map<String,Object> parameter, PayOrder payOrder){
        //消息内容到底应该长成什么样子？
        rocketMQTemplate.sendMessageInTransaction(
                //事务监听器组名字
                RocketConstants.ORDER_PAY_GROUP_TRANSACTION,
                //主题：标签
                RocketConstants.ORDER_PAY_GROUP_TOPIC + ":" + RocketConstants.ORDER_PAY_GROUP_TAG,
                //消息：用作保存支付单
                MessageBuilder.withPayload(JSON.toJSONString(payOrder)).build(),
                //参数：用作保存课程订单和明细
                parameter);
    }
}
